package com.yinhe.music.yhmusic.video;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.model.MvsInfo;
import com.yinhe.music.yhmusic.utils.DataHolder;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/5/11.
 */

public class MvManagerActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private MvAdapter mAdapter;
    private List<MvsInfo> list = new ArrayList<>();
    private List<MvsInfo> deleteList = new ArrayList<>();
    private String deleteId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        RxBus.get().register(this);
        list = DataHolder.getInstance().getData();
        setToolbar("批量操作");
        initBottomMenu();
        setmRecyclerView();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    private void initBottomMenu(){
        TextView play   = findViewById(R.id.play);
        TextView down   = findViewById(R.id.down);
        TextView addTo  = findViewById(R.id.add_to);
        TextView delete = findViewById(R.id.delete);
        play.setVisibility(View.GONE);
        down.setVisibility(View.GONE);
        addTo.setVisibility(View.GONE);
        delete.setOnClickListener(this);
    }


    private void setmRecyclerView(){
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this));
        mAdapter = new MvAdapter(list);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST,0,true));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.delete){
            deleteList.clear();
            StringBuilder mvIds = new StringBuilder();
            List<MvsInfo> albumInfoList = mAdapter.getData();
            for (MvsInfo mvsInfo:albumInfoList){
                if (mvsInfo.isSelect()){
                    mvIds.append(mvsInfo.getMovieId()).append(",");
                    deleteList.add(mvsInfo);
                }
            }

            if (mvIds.length()>0){
                mvIds = new StringBuilder(mvIds.substring(0, mvIds.length() - 1));
                deleteId = mvIds.toString();
                showDeleteDialog(mvIds.toString());
            }
        }
    }


    private class MvAdapter extends BaseQuickAdapter<MvsInfo, BaseViewHolder> {

        MvAdapter(@Nullable List<MvsInfo> data) {
            super(R.layout.fragment_menu_manager,data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final MvsInfo item) {
            helper.setText(R.id.menu_name,item.getMovieName())
                    .setText(R.id.menu_sum,item.getSingerName());

            ImageView image = helper.getView(R.id.song_menu_img);
            GlideHelper.setImageResource(image,item.getImage(),R.drawable.default_cover);

            helper.itemView.setOnClickListener(v -> {
                if (item.isSelect()){
                    item.setSelect(false);
                    helper.setChecked(R.id.checkbox,false);
                }else {
                    item.setSelect(true);
                    helper.setChecked(R.id.checkbox,true);
                }
            });

            CheckBox checkBox = helper.getView(R.id.checkbox);
            checkBox.setOnCheckedChangeListener((compoundButton, b) -> item.setSelect(b));
            checkBox.setChecked(item.isSelect());
        }
    }

    private void showDeleteDialog(final String ids){
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示");
        dialog.setMessage("是否删除");
        dialog.setPositiveButton("确定", (dialogInterface, i) -> reqSetUnLike(ids));
        dialog.setNegativeButton("取消", null);
        dialog.show();
    }

    private void removeMvItem(List<MvsInfo> list) {
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                for (int j= 0;j<deleteList.size();j++){
                    long id = deleteList.get(j).getMovieId();
                    if (list.get(i).getMovieId() == id) {
                        mAdapter.remove(i);
                    }
                }
            }
        }
    }

    private void reqSetUnLike(final String ids) {
//        DefaultReqParam param = new DefaultReqParam();
//        param.put("mvid", ids);
//        mService.collect(ReqConstants.UrlConst.USER_UN_LIKE, param,REQUEST_TAG);
    }

    private void handleLikeResp(Message message){
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        removeMvItem(list);
        RxBus.get().post(RxBusEventType.Mv.UPDATA_COLLECT_NUM, deleteId);
    }
}
